<template>
	<scroll-view scroll-y class="bg-sv bg-color padding-sm">
		<view class="">
			<text
				@click="init(cate.categoryId)"
				v-for="(cate, index) in categorys" :key="index"
			 class="text-color radius cu-tag bg-color border-color">{{cate.name}}</text>
		</view>
		<view>
			<view v-if="products.length <= 0" class="text-center padding">
				<text class="text-color">暂无数据</text>
			</view>
		</view>
		<view  class="cu-card dynamic">
			<view @tap="toProductDetail(product.productId)" v-for="(product,index) in products" :key="index" class="cu-item shadow bg-color  solid-color">
				<view class="text-color margin-sm">
					<text 
						v-for="(cate, kndex) in JSON.parse(product.categorys)" 
						:key="kndex">
						<text v-if="kndex > 0">/</text>
						{{cate.name.replaceAll("■", "")}}
					</text>/
				{{product.name}}
				</view>
				<view class="flex-sub">
					<view class="bg-img only-img" :style="'background-image:url(https://'+product.url+');'">
					</view>
				</view>
			</view>
			<view>
				<view v-if="pageFlg" @click="searchPage" class="text-center padding">
					<text class="text-color">显示更多</text>
				</view>
			</view>
		</view>
	</scroll-view>
</template>

<script>
	export default {
		data: function() {
			return {
				products: [],
				searchKey: "",
				categoryId: "",
				categorys: [],
				current: 1,
				size: 5,
				pageFlg: false
			}
		},
		onLoad:function(option) {
			if (option.categoryId) {
				this.categoryId = option.categoryId;
				this.loadCategory();
			}
			this.init();
		},
		methods: {
			toProductDetail: function(productId) {
				uni.navigateTo({
					url: "../price/detail?productId="+productId
				});
			},
			init: function(categoryId) {
				if (categoryId) {
					this.categoryId = categoryId;
				}
				this.current = 1;
				this.size = 5;
				this.products = [];
				this.loadData();
			},
			loadCategory: function() {
				this.http.get({
					url: "/category/sub",
					param: {
						categoryId: this.categoryId
					}
				}).then(res => {
					this.categorys = res;
				});
			},
			searchPage: function() {
				this.current = this.current + 1;
				this.size = 5;
				this.loadData();
			},
			loadData: function() {
				let self = this;
				this.http.get({
					url: "/product/page",
					param: {
						categoryId: this.categoryId,
						current: this.current,
						size: this.size
					}
				}).then(res => {
					self.current = res.current;
					for (let index in res.records) {
						self.products.push(res.records[index]);
					}
					if (res.current < res.pages) {
						self.pageFlg = true;
					} else {
						self.pageFlg = false;
					}
				});
			}
		}
	}
</script>

<style>
</style>
